// pages/home/home.js
let d = require('../../utils/dataProvince');
Page({

    /**
     * 页面的初始数据
     */
    data: {
      latitude: null,
      longitude: null,
      city: null,
      province: null,
      news: [],
      provincelist: {
        chinaTotal:{}
      },
      citylist: {
        chinaTotal:{}
      },
      provincelist: {
        chinaTotal:{}
      },
      citylist: {
        chinaTotal:{}
      },
     
      
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
      this.getData2()
  setTimeout(()=>{
    this.getDate()
  },1000)
      this.getDw()
     
    },
    getData2() {
      wx.request({
        url: 'https://api.tianapi.com/ncov/index', //仅为示例，并非真实接口地址。
        data: {
          key: 'e7c54e447c3e0c326010ec5f5853f616'
        },
        success: (res) => {
          res.data.newslist[0].news.forEach((item=>{
            item.time =this.formDate(new Date(item.pubDate))
          }))
          this.setData({
            news :res.data.newslist[0].news
          })
        }
      });
    
    },
    getDw() {
      wx.getLocation({
          type: 'wgs84',
          success: (res) => {
              this.setData({
                  latitude: res.latitude,
                  longitude: res.longitude
              })
              wx.request({
                  url: "https://apis.map.qq.com/ws/geocoder/v1",
                  data: {
                      location: res.latitude + ',' + res.longitude,
                      key: 'XUEBZ-HNWL4-HYIUQ-D4QF6-JMZR3-WSBKA',
                      // 配置中你的什么key 这里就请问什么key 不要张三李四
                  },
                  success: (r => {
                    console.log(r);
                      let city = r.data.result.address_component.city
                      let province = r.data.result.address_component.province

                      this.setData({
                          city: city,
                          province: province
                      })

                  })
              })
          }
      })

  },

  
    getDate() {
      wx.request({
        url: "https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=statisGradeCityDetail,diseaseh5Shelf",
        success: (res) => {
         
          res.data.data.diseaseh5Shelf.areaTree[0].children.forEach((item) => {
          
              let chinalist1 = res.data.data.diseaseh5Shelf
            if (item.name +'省' == this.data.province||item.name+"市"==this.data.province) {
            let provincelist1 = item.total
              this.setData({
                provincelist:provincelist1,
              })
              item.children.forEach((i) => {
                
                if (i.name +'市' == this.data.city) {
                  
                let citylist1 = i.total
                this.setData({
                  citylist:citylist1
                })
                }
              })
              this.setData({
                chinalist :chinalist1,
                })
            }
           
          })
          // console.log(res.data.data.diseaseh5Shelf.areaTree[0].children[16].children[1])
          // console.log(res.data.data.diseaseh5Shelf.areaTree[0].children[16])
           
        }
       
      })

    },
    //分享 功能
      onShareAppMessage(){
       return {
        title:'寄',//分享标题
        type:0,
        imageUrl:'',
       }
      },
      formDate(t){
		
        // year 
        // 写完 以后就直接复制
        
        // new Date()
        
        let year  = t.getFullYear();
        
        let mon = Number(t.getMonth()+1)<10?'0'+Number(t.getMonth()+1):Number(mont.getMonth()+1)
        let day  = t.getDate()<10?'0'+t.getDate():t.getDate()
        
        let hour  = t.getHours()<10?'0'+t.getHours():t.getHours();
        let min   = t.getMinutes()<10?'0'+t.getMinutes():t.getMinutes();
        let sec  = t.getSeconds()<10?'0'+t.getSeconds():t.getSeconds()
        return year + '-' + mon +'-'+day +' ' + hour +':'+min +':'+sec
        
      
    },
   

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})